package com.itheima.sfbx.insurance.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.sfbx.framework.commons.basic.ResponseResult;
import com.itheima.sfbx.framework.commons.utils.ResponseResultBuild;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "系数项管理")
@RestController
@RequestMapping("/coefficent")
public class CoefficientController {
    @Resource
    private ICoefficentService coefficentService;

    @ApiOperation(value = "分页", notes = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "VO对象", required = true, dataType = "CoefficentVO"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"coefficentVO.sortNo", "coefficentVO.coefficentKey", "coefficentVO.coefficentKeyName", "coefficentVO.coefficentVal", "coefficentVO.coefficentType"})
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<Page<CoefficentVO>> findCoefficientVoPage(
            @RequestBody CoefficentVO coefficentVO,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        Page<CoefficentVO> coefficentVOPage = coefficentService.findPage(coefficentVO, pageNum, pageSize);
        return ResponseResultBuild.successBuild(coefficentVOPage);
    }

    @ApiOperation(value = "保存", notes = "添加系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "VO对象", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(includeParameters = {"coefficentVO.dataState", "coefficentVO.sortNo", "coefficentVO.coefficentKey", "coefficentVO.coefficentKeyName", "coefficentVO.coefficentVal", "coefficentVO.coefficentType"})
    @PutMapping
    public ResponseResult<CoefficentVO> createCoefficent(@RequestBody CoefficentVO coefficentVO) {
        CoefficentVO coefficentVoResult = coefficentService.save(coefficentVO);
        return ResponseResultBuild.successBuild(coefficentVoResult);
    }

    @ApiOperation(value = "修改", notes = "修改系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "VO对象", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(includeParameters = {"coefficentVO.id", "coefficentVO.dataState", "coefficentVO.sortNo", "coefficentVO.coefficentKey", "coefficentVO.coefficentKeyName", "coefficentVO.coefficentVal", "coefficentVO.coefficentType"})
    @PatchMapping
    public ResponseResult<Boolean> updateCoefficent(@RequestBody CoefficentVO coefficentVO) {
        Boolean flag = coefficentService.update(coefficentVO);
        return ResponseResultBuild.successBuild(flag);
    }

    @ApiOperation(value = "删除", notes = "删除系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "VO对象", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(includeParameters = {"coefficentVO.checkedIds"})
    @DeleteMapping
    public ResponseResult<Boolean> deleteCoefficent(@RequestBody CoefficentVO coefficentVO) {
        Boolean flag = coefficentService.delete(coefficentVO.getCheckedIds());
        return ResponseResultBuild.successBuild(flag);
    }
    @ApiOperation(value = "系数项列表", notes = "系数项列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "VO对象", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(includeParameters = {"coefficentVO.coefficentType"})
    @PostMapping("/list")
    public ResponseResult<List<CoefficentVO>> coefficentList(@RequestBody CoefficentVO coefficentVO){
        List<CoefficentVO> coefficentServiceList = coefficentService.findList(coefficentVO);
        return ResponseResultBuild.successBuild(coefficentServiceList);
    }
    @ApiOperation(value = "系数项key查询", notes = "系数项key查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentKey", value = "系数项key", required = true, dataType = "String")
    })
    @ApiOperationSupport(includeParameters = {"coefficentKey"})
    @PostMapping("/{coefficentKey}")
    public ResponseResult<CoefficentVO> findByCoefficentKey(@PathVariable("coefficentKey") String coefficentKey) {
        CoefficentVO coefficentVO = coefficentService.findByCoefficentKey(coefficentKey);
        return ResponseResultBuild.successBuild(coefficentVO);
    }
}
